/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.client.dialog.setup.view;

import java.util.ResourceBundle;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.Border;
import offset.nodes.client.dialog.setup.controller.SetupActions;
import offset.nodes.client.dialog.setup.controller.SetupActions.NextAction;
import offset.nodes.client.dialog.newnode.model.ObjectType;
import offset.nodes.client.dialog.newnode.model.SetupModel;
import offset.nodes.client.dialog.newnode.model.Step;

/**
 *
 * @author  Walter Lütgenau
 */
public class SetupDialog extends JDialog {
    SetupModel model;
    /** A return status code - returned if Cancel button has been pressed */
    public static final int RET_CANCEL = 0;
    /** A return status code - returned if OK button has been pressed */
    public static final int RET_OK = 1;
    private int returnStatus = RET_CANCEL;
    
    /** Creates new form NewNodeDialog */
    public SetupDialog(java.awt.Frame parent, boolean modal, SetupModel model) {
        super(parent, "", modal);
        
        this.model = model;
        initComponents();
        
        //if ( model.getObjectTypes().length > 1)
            addFirst(model, new SelectObjectTypePanel(model), "selectObjectType.name");
        
        for( int i=0; i<model.getObjectTypes().length; i++) {
            ObjectType objectType = model.getObjectTypes()[i];
            for( int j=0; j<objectType.getSteps().length; j++) {
                Step step = objectType.getSteps()[j];
                JScrollPane scroll = new JScrollPane();
                scroll.setViewportView(step.getPanel());
                scroll.setBorder(null);
                currentStepPanel.add(scroll, step.getKey());
            }
        }

        Step currentStep = model.getObjectTypes()[model.getCurrentObjectType()].getSteps()[model.getCurrentStep()];
        //currentStepPanel.add(currentStep.getPanel(), currentStep.getName() );
        currentStepLabel.setText(currentStepPanel.getName());
        
        ((NextAction)nextButton.getAction()).update();
    }
    
    protected void addFirst(SetupModel model, JPanel panel, String name) {
        for( int i=0; i<model.getObjectTypes().length; i++) {
            ObjectType type = model.getObjectTypes()[i];
            
            Step[] newSteps = new Step[type.getSteps().length+1];
            newSteps[0] = new Step(ResourceBundle.getBundle("offset/nodes/client/dialog/view/DialogBundle"), name, panel );
            for( int j=0; j<type.getSteps().length; j++) 
                newSteps[j+1] = type.getSteps()[j];
            
            type.setSteps(newSteps);
        }
        
        model.setCurrentStep(0);
        model.setCurrentObjectType(0);
    }
    
   public void doClose(int retStatus) {
        returnStatus = retStatus;
        setVisible(false);
        dispose();
    }
    
    public SetupModel getModel() {
        return model;
    }

    public javax.swing.JButton getBackButton() {
        return backButton;
    }

    public void setBackButton(javax.swing.JButton backButton) {
        this.backButton = backButton;
    }

    public javax.swing.JPanel getButtonPanel() {
        return buttonPanel;
    }

    public void setButtonPanel(javax.swing.JPanel buttonPanel) {
        this.buttonPanel = buttonPanel;
    }

    public javax.swing.JButton getCancelButton() {
        return cancelButton;
    }

    public void setCancelButton(javax.swing.JButton cancelButton) {
        this.cancelButton = cancelButton;
    }

    public javax.swing.JLabel getCurrentStepLabel() {
        return currentStepLabel;
    }

    public void setCurrentStepLabel(javax.swing.JLabel currentStepLabel) {
        this.currentStepLabel = currentStepLabel;
    }

    public javax.swing.JPanel getCurrentStepPanel() {
        return currentStepPanel;
    }

    public void setCurrentStepPanel(javax.swing.JPanel currentStepPanel) {
        this.currentStepPanel = currentStepPanel;
    }

    public javax.swing.JButton getFinishButton() {
        return finishButton;
    }

    public void setFinishButton(javax.swing.JButton finishButton) {
        this.finishButton = finishButton;
    }

    public javax.swing.JButton getNextButton() {
        return nextButton;
    }

    public void setNextButton(javax.swing.JButton nextButton) {
        this.nextButton = nextButton;
    }

    public javax.swing.JPanel getStepsOverviewPanel() {
        return stepsOverviewPanel;
    }

    public void setStepsOverviewPanel(javax.swing.JPanel stepsOverviewPanel) {
        this.stepsOverviewPanel = stepsOverviewPanel;
    }

    public javax.swing.JLabel getJLabel1() {
        return jLabel1;
    }

    public void setJLabel1(javax.swing.JLabel jLabel1) {
        this.jLabel1 = jLabel1;
    }

    public javax.swing.JLabel getJLabel2() {
        return jLabel2;
    }

    public void setJLabel2(javax.swing.JLabel jLabel2) {
        this.jLabel2 = jLabel2;
    }

    public javax.swing.JLabel getJLabel3() {
        return jLabel3;
    }

    public void setJLabel3(javax.swing.JLabel jLabel3) {
        this.jLabel3 = jLabel3;
    }

    public javax.swing.JLabel getJLabel4() {
        return jLabel4;
    }

    public void setJLabel4(javax.swing.JLabel jLabel4) {
        this.jLabel4 = jLabel4;
    }

    public javax.swing.JLabel getJLabel5() {
        return jLabel5;
    }

    public void setJLabel5(javax.swing.JLabel jLabel5) {
        this.jLabel5 = jLabel5;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonPanel = new javax.swing.JPanel();
        jSeparator1 = new javax.swing.JSeparator();
        cancelButton = new javax.swing.JButton();
        finishButton = new javax.swing.JButton();
        nextButton = new javax.swing.JButton();
        backButton = new javax.swing.JButton();
        stepsOverviewPanel = new javax.swing.JPanel();
        stepsLabel = new javax.swing.JLabel();
        jSeparator2 = new javax.swing.JSeparator();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        currentStepPanel = new javax.swing.JPanel();
        jSeparator3 = new javax.swing.JSeparator();
        currentStepLabel = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        cancelButton.setAction(new SetupActions.CancelAction(getModel(),this));
        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("offset/nodes/client/dialog/view/DialogBundle"); // NOI18N
        cancelButton.setText(bundle.getString("newNode.cancel")); // NOI18N

        finishButton.setAction(new SetupActions.FinishAction(getModel(), this));
        finishButton.setText(bundle.getString("newNode.finish")); // NOI18N

        nextButton.setAction(new SetupActions.NextAction(getModel(), this));
        nextButton.setText(bundle.getString("newNode.next")); // NOI18N

        backButton.setAction(new SetupActions.PreviousAction(getModel(), this));
        backButton.setText(bundle.getString("newNode.back")); // NOI18N

        javax.swing.GroupLayout buttonPanelLayout = new javax.swing.GroupLayout(buttonPanel);
        buttonPanel.setLayout(buttonPanelLayout);
        buttonPanelLayout.setHorizontalGroup(
            buttonPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, buttonPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(buttonPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 650, Short.MAX_VALUE)
                    .addGroup(buttonPanelLayout.createSequentialGroup()
                        .addComponent(backButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(nextButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(finishButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cancelButton)))
                .addGap(20, 20, 20))
        );
        buttonPanelLayout.setVerticalGroup(
            buttonPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, buttonPanelLayout.createSequentialGroup()
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(buttonPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cancelButton)
                    .addComponent(finishButton)
                    .addComponent(nextButton)
                    .addComponent(backButton))
                .addContainerGap())
        );

        stepsOverviewPanel.setBackground(java.awt.Color.white);
        stepsOverviewPanel.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));

        stepsLabel.setFont(new java.awt.Font("Microsoft Sans Serif", 1, 11));
        stepsLabel.setText(bundle.getString("newNode.steps")); // NOI18N

        jSeparator2.setForeground(java.awt.Color.black);

        jLabel1.setText("\n");

        javax.swing.GroupLayout stepsOverviewPanelLayout = new javax.swing.GroupLayout(stepsOverviewPanel);
        stepsOverviewPanel.setLayout(stepsOverviewPanelLayout);
        stepsOverviewPanelLayout.setHorizontalGroup(
            stepsOverviewPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(stepsOverviewPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(stepsOverviewPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(stepsLabel)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5)
                    .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 166, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(13, Short.MAX_VALUE))
        );
        stepsOverviewPanelLayout.setVerticalGroup(
            stepsOverviewPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(stepsOverviewPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(stepsLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel5)
                .addContainerGap())
        );

        currentStepPanel.setLayout(new java.awt.CardLayout());

        jSeparator3.setBackground(javax.swing.UIManager.getDefaults().getColor("Button.background"));
        jSeparator3.setForeground(java.awt.Color.black);

        currentStepLabel.setFont(new java.awt.Font("Microsoft Sans Serif", 1, 11));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(stepsOverviewPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(currentStepLabel)
                    .addComponent(jSeparator3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 461, Short.MAX_VALUE)
                    .addComponent(currentStepPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 461, Short.MAX_VALUE))
                .addContainerGap())
            .addComponent(buttonPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(21, 21, 21)
                        .addComponent(currentStepLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jSeparator3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(currentStepPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 320, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(stepsOverviewPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 344, Short.MAX_VALUE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(buttonPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    public int getReturnStatus() {
        return returnStatus;
    }

    public void setReturnStatus(int returnStatus) {
        this.returnStatus = returnStatus;
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton backButton;
    private javax.swing.JPanel buttonPanel;
    private javax.swing.JButton cancelButton;
    private javax.swing.JLabel currentStepLabel;
    private javax.swing.JPanel currentStepPanel;
    private javax.swing.JButton finishButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JButton nextButton;
    private javax.swing.JLabel stepsLabel;
    private javax.swing.JPanel stepsOverviewPanel;
    // End of variables declaration//GEN-END:variables
    
}
